package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import play.data.validation.Required;
import play.db.jpa.Model;

/**
 * Holds all attributes of a Rating that could be added to an idea, topic,
 * action plan, organization or entity with the idea developer who rated.
 * 
 * @author Shahir
 */
@Entity
public class Rate extends Model {

	@Required
	public int rating;
	@Required
	@ManyToOne
	IdeaDeveloper ideaDeveloper;

	@ManyToOne
	public Idea idea = null;

	@ManyToOne
	public Topic topic = null;

	@ManyToOne
	public ActionPlan actionPlan = null;

	@ManyToOne
	public Organization organization = null;

	@ManyToOne
	public Entities entity = null;

	/**
	 * 
	 * Constructs a new Rate by an idea developer for a certain idea.
	 * 
	 * @author Shahir
	 * 
	 * @param rating
	 *            The Integer representing the rating chosen by the idea developer.
	 * @param ideaDeveloper
	 *            The ideaDeveloper attempting to rate.
	 * @param idea
	 *            The Idea that is being rated.
	 */

	public Rate(int rating, IdeaDeveloper ideaDeveloper, Idea idea) {
		this.rating = rating;
		this.ideaDeveloper = ideaDeveloper;
		this.idea = idea;
		this.topic = null;
		this.actionPlan = null;
		this.organization = null;
		this.entity = null;
        this.idea.rates.add(this);
        
        Log log = new Log("^ rated ^.");
		log.ids = ideaDeveloper.id + "," + idea.id;
		log.types = "developer,idea";
		log.save();
	}

	/**
	 * 
	 * Constructs a new Rate by an idea developer for a certain topic.
	 * 
	 * @author Shahir
	 * 
	 * @param rating
	 *            The Integer representing the rating chosen by the idea developer.
	 * @param ideaDeveloper
	 *            The ideaDeveloper attempting to rate.
	 * @param topic
	 *            The Topic that is being rated.
	 */

	public Rate(int rating, IdeaDeveloper ideaDeveloper, Topic topic) {
		this.rating = rating;
		this.ideaDeveloper = ideaDeveloper;
		this.idea = null;
		this.topic = topic;
		this.actionPlan = null;
		this.organization = null;
		this.entity = null;
                this.topic.rates.add(this);
                
                Log log = new Log("^ rated ^.");
        		log.ids = ideaDeveloper.id + "," + topic.id;
        		log.types = "developer,topic";
        		log.save();
	}

	/**
	 * 
	 * Constructs a new Rate by an idea developer for a certain action plan.
	 * 
	 * @author Shahir
	 * 
	 * @param rating
	 *            The Integer representing the rating chosen by the idea developer.
	 * @param ideaDeveloper
	 *            The ideaDeveloper attempting to rate.
	 * @param actionPlan
	 *            The ActionPlan that is being rated.
	 */

	public Rate(int rating, IdeaDeveloper ideaDeveloper, ActionPlan actionPlan) {
		this.rating = rating;
		this.ideaDeveloper = ideaDeveloper;
		this.idea = null;
		this.topic = null;
		this.actionPlan = actionPlan;
		this.organization = null;
		this.entity = null;
                this.actionPlan.rates.add(this);
                
                
                Log log = new Log("^ rated ^.");
        		log.ids = ideaDeveloper.id + "," + actionPlan.id;
        		log.types = "developer,actionplan";
        		log.save();
	}

	/**
	 * 
	 * Constructs a new Rate by an idea developer for a certain organization.
	 * 
	 * @author Shahir
	 * 
	 * @param rating
	 *            The Integer representing the rating chosen by the idea developer.
	 * @param ideaDeveloper
	 *            The ideaDeveloper attempting to rate.
	 * @param organization
	 *            The Organization that is being rated.
	 */

	public Rate(int rating, IdeaDeveloper ideaDeveloper,
			Organization organization) {
		this.rating = rating;
		this.ideaDeveloper = ideaDeveloper;
		this.idea = null;
		this.topic = null;
		this.actionPlan = null;
		this.organization = organization;
		this.entity = null;
                this.organization.rates.add(this);
                
                Log log = new Log("^ rated ^.");
        		log.ids = ideaDeveloper.id + "," + organization.id;
        		log.types = "developer,organization";
        		log.save();
	}

	/**
	 * 
	 * Constructs a new Rate by an idea developer for a certain entity.
	 * 
	 * @author Shahir
	 * 
	 * @param rating
	 *            The Integer representing the rating chosen by the idea developer.
	 * @param ideaDeveloper
	 *            The ideaDeveloper attempting to rate.
	 * @param entity
	 *            The Entities that is being rated.
	 */

	public Rate(int rating, IdeaDeveloper ideaDeveloper, Entities entity) {
		this.rating = rating;
		this.ideaDeveloper = ideaDeveloper;
		this.idea = null;
		this.topic = null;
		this.actionPlan = null;
		this.organization = null;
		this.entity = entity;
                this.entity.rates.add(this);
                
                Log log = new Log("^ rated ^.");
        		log.ids = ideaDeveloper.id + "," + entity.id;
        		log.types = "developer,entity";
        		log.save();
	}

	/**
	 * this method deletes the rate and delete the corresponding data from all
	 * other tables
	 * 
	 * @author KimO
	 */
	public void delete2() {
		ideaDeveloper.rates.remove(this);
		ideaDeveloper.save();
		if (idea != null) {
			idea.rates.remove(this);
			idea.save();
		} else if (topic != null) {
			topic.rates.remove(this);
			topic.save();
		} else if (actionPlan != null) {
			actionPlan.rates.remove(this);
			actionPlan.save();
		} else if (organization != null) {
			organization.rates.remove(this);
			organization.save();
		} else if (entity != null) {
			entity.rates.remove(this);
			entity.save();
		}
		this.delete();
	}

}
